package com.hp.ongoing.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;

public final class FileUtil {

	
	public static String getRelativePath(String pathfile) {
		URL url = ClassLoader.getSystemResource(pathfile);
		return url.getPath();
	}
	
	public static String getContentFileAsString(String filePath){
		InputStream is = getFilePathAsStream(filePath); 
		return getStreamToString(is);
	}
	
	public static InputStream getFilePathAsStream(String filePath){
		InputStream is = ClassLoader.getSystemResourceAsStream(filePath);
		return is;
	}
	
	private static String getStreamToString(InputStream is){
		
		//intilize an InputStream
    	
 
    	//read it with BufferedReader
    	BufferedReader br
        	= new BufferedReader(
        		new InputStreamReader(is));
 
    	StringBuilder sb = new StringBuilder();
 
    	String line;
    	try {
			while ((line = br.readLine()) != null) {
				sb.append(line);
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
 
    	System.out.println(sb.toString());
 
    	try {
			br.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return sb.toString();
	}

	
}
